package cn.exrick.xboot.modules.gatestation.controller;

import cn.exrick.xboot.base.XbootBaseController;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.gatestation.entity.NaturalgasDashboard;
import cn.exrick.xboot.modules.gatestation.entity.NaturalgasEquipment;
import cn.exrick.xboot.modules.gatestation.service.NaturalgasDashboardService;
import cn.exrick.xboot.modules.gatestation.service.NaturalgasEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author yck
 */
@Slf4j
@RestController
@Api(description = "天燃气前端仪表盘设置管理接口")
@RequestMapping("/xboot/naturalgasDashboard")
@Transactional
public class NaturalgasDashboardController extends XbootBaseController<NaturalgasDashboard, String> {

    @Autowired
    private NaturalgasDashboardService naturalgasDashboardService;

    @Autowired
    private NaturalgasEquipmentService naturalgasEquipmentService;

    @Override
    public NaturalgasDashboardService getService() {
        return naturalgasDashboardService;
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<NaturalgasDashboard>> getByCondition(NaturalgasDashboard naturalgasDashboard,
                                                            SearchVo searchVo,
                                                            PageVo pageVo){

        Page<NaturalgasDashboard> page = naturalgasDashboardService.findByCondition(naturalgasDashboard, searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<Page<NaturalgasDashboard>>().setData(page);
    }
    @RequestMapping(value = "/getByGateStationId", method = RequestMethod.GET)
    @ApiOperation(value = "获取下拉数据")
    public  Result<Object> getByGateStationId(String gateStationId){
        List<NaturalgasEquipment> byGateStationId = naturalgasEquipmentService.findByGateStationId(gateStationId);
        return new ResultUtil<Object>().setData(byGateStationId);
    }
    //saveDashboardEquipment
    @RequestMapping(value = "/saveDashboardEquipment", method = RequestMethod.POST)
    @ApiOperation(value = "保存设置好的数据")
    public  Result<Object> saveDashboardEquipment(String gateStationId,String dashboard1,String dashboard2){
        List<NaturalgasDashboard> byGateStationId = naturalgasDashboardService.findByGateStationId(gateStationId);
        if(byGateStationId.size()>0){
            byGateStationId.get(0).setDashboard1(dashboard1);
            byGateStationId.get(0).setDashboard2(dashboard2);
            naturalgasDashboardService.update(byGateStationId.get(0));
        }else{
            NaturalgasDashboard naturalgasDashboard=new NaturalgasDashboard();
            naturalgasDashboard.setGateStationId(gateStationId);
            naturalgasDashboard.setDashboard1(dashboard1);
            naturalgasDashboard.setDashboard2(dashboard2);
            naturalgasDashboardService.save(naturalgasDashboard);
        }
        return new ResultUtil<Object>().setSuccessMsg("保存成功！");
    }
    //根据门站获取仪表盘
    @RequestMapping(value = "/getNaturalgasDashboardByGateStationId", method = RequestMethod.GET)
    @ApiOperation(value = "获取下拉数据")
    public  Result<Object> getNaturalgasDashboardByGateStationId(String gateStationId){
        List<NaturalgasDashboard> byGateStationId = naturalgasDashboardService.findByGateStationId(gateStationId);
        if(byGateStationId.size()>0){
            return new ResultUtil<Object>().setData(byGateStationId.get(0));
        }else{
            return new ResultUtil<Object>().setSuccessMsg("null");
        }

    }
}
